<template>
  <el-form ref="form" :rules="rules" label-width="100px">
    <el-form-item label="步骤名称" prop="name">
      <el-input v-model="step.name" placeholder="步骤名称"></el-input>
    </el-form-item>

    <el-button type="primary" @click="fetchFieldsFromTable">获取字段</el-button>
    <el-button icon="plus" @click="_addRecord"></el-button>
    <br><br>
    <el-table :data="step.fields" stripe :max-height="300">
      <el-table-column type="index" :width="60"></el-table-column>
      <el-table-column label="字段名称" :width="120">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.field_name}}</div>
          <div v-else>
            <el-select v-model="scope.row.field_name">
              <el-option v-for="item in step.availableFields" :value="item.name"></el-option>
            </el-select>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="变量名" :width="120">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.variable_name}}</div>
          <div v-else>
            <el-input v-model="scope.row.variable_name" ></el-input>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="变量存放类型" :width="120">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.variable_type}}</div>
          <div v-else>
            <el-select  v-model="scope.row.variable_type">
              <el-option v-for="item in options.variableType" :value="item.value" :label="item.name"></el-option>
            </el-select>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="默认值" :width="120">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.default_value}}</div>
          <div v-else>
            <el-input  v-model="scope.row.default_value"></el-input>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="操作" :width="140">
        <template scope="scope">
          <el-row>
            <el-col :span="12"><el-button size="small" type="info" @click="reverseEditState(scope.$index, scope.row)">编辑</el-button>
            </el-col>
            <el-col :span="12"><el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
</template>

<script>
  import {Steps,Fields,Options} from "../../model/DataModel";
  import StepCommon from "./mixins/StepMixin";

  export default{
    mixins: [StepCommon],
    data(){
      return {
        step:Steps.SetVariable(),
        options:Options
      }
    },
    methods:{
      fetchFieldsFromTable(){
         this.step.fields=this.step.availableFields.map(el=>Fields.SetVariable({field_name:el.name}))
      }
    }
  }
</script>
